﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Devart.Data.SQLite;

namespace ExpreRec.DataAccess.Util
{
    /// <summary>
    /// SqlDataReader包装类，当记录为空时返回null，而不抛出异常
    /// </summary>
    public class SafeSQLiteDataReader
    {
        private SQLiteDataReader reader = null;
        public int FieldCount
        {
            get
            {
                return reader.FieldCount;
            }
        }

        public SafeSQLiteDataReader(SQLiteDataReader reader)
        {
            this.reader = reader;
        }

        public bool Read()
        {
            return reader.Read();
        }

        public object GetValue(int i)
        {
            if(reader.IsDBNull(i))
                return null;
            else return reader.GetValue(i);
        }
        public string GetString(int i)
        {
            if (reader.IsDBNull(i))
                return null;
            else return reader.GetString(i);
        }
        public int GetInt32(int i)
        {
            //如果数据为空则为-1

            if (reader.IsDBNull(i))
                return -1;
            else return reader.GetInt32(i);
        }
        public decimal GetDecimal(int i)
        {
            if (reader.IsDBNull(i))
                return 0;
            else return reader.GetDecimal(i);
        }
        public bool GetBoolean(int i)
        {
            if (reader.IsDBNull(i))
                return false;
            else return reader.GetBoolean(i);
        }
        public DateTime GetDateTime(int i)
        {
            if (reader.IsDBNull(i))
                return new DateTime(0);
            else return reader.GetDateTime(i);
        }
    }
}
